import React, {useState,useEffect} from "react";
import { trim,map} from "lodash";
import {Modal, Form,Checkbox, Row,Col, Input } from "antd/lib";
import { wrap as wrapDialog, DialogPropType } from "@/components/DialogWrapper";
import DynamicComponent from "@/components/DynamicComponent";
import {policy} from "@/services/policy";
import {UserProfile} from "@/components/proptypes";
import Attaches from "@/services/attaches";
import notification from "@/services/notification";
import {currentUser} from "@/services/auth";

function CreateAnnexDialog({ orgunitid, dialog }) {
  const [dataLoaded, setDataLoaded] = useState(orgunitid ===0 ? true : false);
  const [annex, setannex] = useState([]);
  const [annexchk, setannexchk] = useState([]);
  const [isValid, setIsValid] = useState(false);
  const [saveInProgress, setSaveInProgress] = useState(false);
  const isCreateStateEnabled = useState(true);//policy.isAdmin() || policy.isDefault();

  useEffect(() => {
    let params={
      sourcetype: "noticeorg",
      sourceid: currentUser.orgunit_id,
      containpublic: 0,
      userid: currentUser.id,
    }
    let cancelLoading = false;
    if (!cancelLoading) {
    Attaches.getlistsel(params).then(data => {
          setannex(data); 
          setDataLoaded(true);
      });
    }
  return () => {
    cancelLoading = true;
  };
  }, [orgunitid]);


  function handleannexChange(checkedValues) {
    setSaveInProgress(false);
    setannexchk(checkedValues);
  }
  function handleSubmit() {
    setSaveInProgress(true);
    let annexin=[];
    if(annexchk.length>0){
      annexchk.forEach((item)=>{
        annex.forEach((node)=>{
            if(item===node.annexurl){
              if(node.annexurl.indexOf("static/file/")<0){
                node.annexurl='static/file/'+node.annexurl;
              }
              const b=[{
                uid: node.uid,
                name: node.annexname,
                status: "done",
                bytstatus: 0,
                nature:0,
                url: node.annexurl
                  }];

              if (annexin.length < 1) {
                annexin = b;
              } else {
                annexin.push.apply(annexin, b);
              }
            }
        });
    });
    }
    dialog.close({success:true, annex: annexin});
  }

  return (
    <Modal
      {...dialog.props}
      {...(isCreateStateEnabled ? {} : { footer: null })}
      width={document.querySelector('body').offsetWidth*0.5}
      title="附件"
      okText="确定"
      cancelText="取消"
      // okButtonProps={{
      //   disabled: !isValid || saveInProgress,
      //   loading: saveInProgress,
      //   "data-test": "DashboardSaveButton",
      // }}
      // cancelButtonProps={{
      //   disabled: saveInProgress,
      // }}
      onOk={handleSubmit}
      // closable={!saveInProgress}
      // maskClosable={!saveInProgress}
      wrapProps={{
        "data-test": "CreateDashboardDialog",
      }}>
      {dataLoaded && (
      <DynamicComponent name="CreateAnnexDialogExtra" >
      <Form>
        <Form.Item style={{ marginBottom: 10 }} label="附件名称">
          <Checkbox.Group
            defaultValue={annex}
            onChange={handleannexChange}>
            {map(annex, ds => (
              <Row><Col span={30}><Checkbox value={ds.annexurl}>{ds.annexname}</Checkbox></Col></Row>
            ))}
            
          </Checkbox.Group>
        </Form.Item>
      </Form>
      </DynamicComponent>
      )}
    </Modal>
  );
}

CreateAnnexDialog.propTypes = {
  annex_id: UserProfile.isRequired,
  dialog: DialogPropType.isRequired,
};

export default wrapDialog(CreateAnnexDialog);
